/**
 * 
 */
package com.youxin.cpcenter.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.youxin.busfoundation.common.BusinessDataException;
import com.youxin.cpcenter.dao.TbEntMemberIdentifyCapitalMapper;
import com.youxin.cpcenter.model.TbEntMemberIdentifyCapital;
import com.youxin.cpcenter.service.LeadsNumberService;

/**
 * @description 
 * @author yingde.cao
 * @date 2017年12月12日
 * @version 1.0
 */
@Service("leadsNumberService")
public class LeadsNumberServiceImpl implements LeadsNumberService{
	
	@Autowired
	private TbEntMemberIdentifyCapitalMapper tbEntMemberIdentifyCapitalMapper;
	/* (non-Javadoc)
	 * @see com.youxin.cpcenter.service.LeadsNumberService#isCanConsumer(long)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public boolean isCanConsumer(long entIdentity) {
		TbEntMemberIdentifyCapital tbEntMemberIdentifyCapital=tbEntMemberIdentifyCapitalMapper.selectByPrimaryKey(entIdentity);		
		return tbEntMemberIdentifyCapital.getLeadsNumber().intValue()>0;
	}
	
	/* (non-Javadoc)
	 * @see com.youxin.cpcenter.service.LeadsNumberService#increaseLeadsNumber(long, int)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public boolean increaseLeadsNumber(long entIdentity, int leadsNumber) {
		TbEntMemberIdentifyCapital tbEntMemberIdentifyCapital=new TbEntMemberIdentifyCapital();
		tbEntMemberIdentifyCapital.setId(entIdentity);
		tbEntMemberIdentifyCapital.setUpdateTime(System.currentTimeMillis());
		tbEntMemberIdentifyCapital.setLeadsNumber(leadsNumber);
		int rowEffected=tbEntMemberIdentifyCapitalMapper.increaseLeadsNumber(tbEntMemberIdentifyCapital);
		if(rowEffected!=1)
		{
			throw new BusinessDataException("更新失败");
		}
		return rowEffected==1;
	}

	/* (non-Javadoc)
	 * @see com.youxin.cpcenter.service.LeadsNumberService#decreaseLeadsNumber(long)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public boolean decreaseLeadsNumber(long entIdentity) {
		TbEntMemberIdentifyCapital tbEntMemberIdentifyCapital=new TbEntMemberIdentifyCapital();
		tbEntMemberIdentifyCapital.setId(entIdentity);
		tbEntMemberIdentifyCapital.setUpdateTime(System.currentTimeMillis());
		tbEntMemberIdentifyCapital.setLeadsNumber(1);
		int rowEffected=tbEntMemberIdentifyCapitalMapper.decreaseLeadsNumber(tbEntMemberIdentifyCapital);
		if(rowEffected!=1)
		{
			throw new BusinessDataException("企业没有可用的线索数量");
		}
		return rowEffected==1;		
	}
	
}
